﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ActionPickInplaceEditOrigin.h"
#include "NewInPlaceFamDlg.h"
#include "InplaceEditSharedContent.h"
#include "UiInplaceEditModeProjectUtil.h"
#include "ComponentEditProjectState.h"

// 平台头文件


#include "IUiDocument.h"
#include "IUiView.h"
#include "Vector2dUtils.h"
#include "IPickNodeReferenceAction.h"
#include "IPickFilter.h"
#include "IPickPointAction.h"
#include "ISnapContext.h"
#include "IGraphicsNodeReference.h"
#include "IModelView.h"
#include "GbmpModelViewUtil.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;

ActionPickInplaceEditOrigin::ActionPickInplaceEditOrigin()
    : m_isUserCancelled(false)
    , m_opSnap(nullptr)
{
}

ActionPickInplaceEditOrigin::~ActionPickInplaceEditOrigin()
{
}

void ActionPickInplaceEditOrigin::InitAction(IUiView* pCurrentView)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = pCurrentView->GetModelView();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    // 当前视图工作楼层
    ElementId currentLevelId = GbmpModelViewUtil::GetWorkLevelId(pModelView);
    DBG_WARN_AND_RETURN_VOID_UNLESS(currentLevelId.IsValid(), L"workLevelId无效",L"GDMPLab",L"2024-03-30");

    // 输入在位编辑的类别、族名称信息
    NewInPlaceFamDlg dlg(pCurrentView->GetUiDocument()->GetDbDocument(), currentLevelId);
    if (dlg.exec() != QDialog::Accepted)
    {
        return;
    }
    m_familyName = dlg.GetExchangeData().FamName;
    m_categoryUid = dlg.GetExchangeData().CategoryUid;
    std::wstring workingLevelName = dlg.GetExchangeData().WorkingLevelName;
    gcmp::ElementId selectedWorkLevelId = dlg.GetExchangeData().WorkLevelId;

    // 从对话框中选择的工作楼层
    ComponentEditProjectState::Get()->SetWorkLevelId(selectedWorkLevelId);
    SetAndStartChildAction();
}

bool ActionPickInplaceEditOrigin::OnViewSwitched(IUiView* pUIView)
{
    MarkFinishStatus(ActionFinishStatus::Successful);
    return true;
}

void ActionPickInplaceEditOrigin::OnChildActionFinished(IUiView* pCurrentView, const ActionOutput& childActionReturnParam)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");

    if (m_isUserCancelled)
    {
        MarkFinishStatus(ActionFinishStatus::Successful);
        return;
    }
    gcmp::Vector3d originPt = m_originPt;

    EnterInplaceEditMode(pCurrentView, m_familyName, m_categoryUid, originPt);
    MarkFinishStatus(ActionFinishStatus::Successful);
}

bool ActionPickInplaceEditOrigin::SetAndStartChildAction()
{
    SetPromptMessage(GBMP_TR(L"请指定构件插入点"));
    UpdateView();
    m_isUserCancelled = false;
    m_opSnap = nullptr;

    PickPointExchangeData exchangeDate(&m_originPt, &m_isUserCancelled, &m_pickPoints, &m_opSnap);
    gcmp::OwnerPtr<IPickPointAction> upPickPointAction = IPickPointAction::Create(exchangeDate, nullptr);

    ISnapContext* context = upPickPointAction->GetSnapContext();
    context->SetSnapCandidatesPreprocessor(GetSnapCandidatesPreprocessor());

    upPickPointAction->SetSnapContext(context);
    upPickPointAction->SetCursorType(GetCursorType());

    StartChildAction(TransferOwnership(upPickPointAction));
    return true;
}

bool ActionPickInplaceEditOrigin::EnterInplaceEditMode(IUiView* pCurrentView, const std::wstring& familyName, const gcmp::UniIdentity& categoryUid, const gcmp::Vector3d& originPt)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");
    IUiDocument *pUiDoc = pCurrentView->GetUiDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pUiDoc, L"pUiDoc为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pUiDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IModelView* pModelView = pCurrentView->GetModelView();

    // 共享数据的生成
    OwnerPtr<InplaceEditSharedContent> opInplaceEditSharedContent = NEW_AS_OWNER_PTR(InplaceEditSharedContent);
    opInplaceEditSharedContent->SetCategoryId(categoryUid);
    opInplaceEditSharedContent->SetFamilyName(familyName);
    opInplaceEditSharedContent->SetEditFamilyFilePath(L"");
    opInplaceEditSharedContent->SetInsertionPoint(originPt);
    std::vector<OwnerPtr<InplaceEditSharedModelViewInfo>> viewList;
    UiInplaceEditModeProjectUtil::GetViewList(pDoc, originPt, viewList);
    opInplaceEditSharedContent->SetViewInfos(std::move(viewList));
    opInplaceEditSharedContent->SetCurrentViewName(pModelView->GetName());

    // 开始在位编辑
    UiInplaceEditModeProjectUtil::StartInplaceEditProject(pDoc, TransferOwnership(opInplaceEditSharedContent), true);

    return true;
}

